package cn.stevenyang;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class CommonIO {
    public static String readWholeFile(String path) {
        File file = new File(path);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            StringBuffer sb = new StringBuffer();
            while (true) {
                int num = fis.read();//读取fis对象指定的a.txt文件内的数据
                if (num == -1) {
                    break;
                }
                sb.append((char) num);
            }
            return sb.toString();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //不管前面有没有读取到数据，流最终是要关闭
            try {
                if(fis!=null){
                    fis.close();//关闭流
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
